<?php
/**
 * exclusivecontroller.php
 * 
 * Create on 2015-04-01 17:18:43
 * Create by yuebin
 */
namespace haibao\cms\business\control\pc\index;

use haibao\frame\data\query\Condition;
use haibao\cms\model\filter\Video as VideoFilter;
use haibao\cms\model\filter\ImageLibrary as ImageLibraryFilter;
use \haibao\www\model\filter\ZtSpecial as ZtSpecialFilter;
use \haibao\www\model\data\ZtSpecial as ZtSpecialData;

class ExclusiveController extends \haibao\cms\business\control\pc\channel\ChannelController{
	
	const VIDEO_COUNT = 5;
	const FEATURE_COUNT = 6;
	const TOTAL_COUNT = 11;
	public function __construct($updateTime = null,$controlId,$isPublish){
		parent::__construct($updateTime,$controlId,$isPublish);
	}
	
	/**
	 * 显示模版内容（模版动态数据的绑定和模版输出）
	 * @param : String $content
	 * @param : Array $controlData;
	 */
	public function displayTemplateHtml(){

		$controlData = $this->getParameter(self::$CONTROL_DATA);
		$content = $this->getParameter(self::$CONTROL_CONTENT);
		$parameterValue = array();
		if($controlData['auto']){
			
			// DO : 公共方法解析综合数据(考虑存在复合数据需要剔除)
			$this->parseMultiData($controlData['auto'],$controlData['manual']);
		}
		$prevArr = $nextArr = array();
		list($prevArr,$nextArr) = $this->getFinalData();
		$this->assign('prevArr',$prevArr);
		$this->assign('nextArr',$nextArr);
		
		$this->assign('channelUrl', \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_EXCLUSIVE));
		
		$path = $this->generateHtmlPath(__CLASS__);
		$this->generateHtml($path,$content);

		$content = $this->getHtml($path);
		$content = $this->replaceCDNLink($content);	
		return $content;
	}


	public function getFinalData(){

		$videoArr = $this->getVideoList();
		$featureArr = $this->getFeatureList();
		
		$paramArr = $this->paramPackageArr();
		$dbDataArr = $finalArr = array();
		foreach($videoArr as $key=>$video){
			$dbDataArr[$key] = $video;
		}
		foreach($featureArr as $key=>$feature){
			$dbDataArr[$key] = $feature;
		}
		krsort($dbDataArr);
		if(self::TOTAL_COUNT >=count($paramArr)){
			$dbDataArr = array_slice($dbDataArr, 0,self::TOTAL_COUNT - count($paramArr));
			$finalArr = array_merge($paramArr,$dbDataArr);
		}
		if($finalArr){
			return array(array_slice($finalArr, 0,5),array_slice($finalArr, 5,6));
		}
		return array(array(),array());
	}
	
	/**
	 * 获取最新发布的视频
	 */
	private function getVideoList(){
		
		$videoFilter = new VideoFilter();
		$videoFilter->select(
			array(
				VideoFilter::CONDITION_FIELD_NAME_ID,
				VideoFilter::CONDITION_FIELD_NAME_TITLE,
				VideoFilter::CONDITION_FIELD_NAME_PICID,
				VideoFilter::CONDITION_FIELD_NAME_PUBLISHDATE,
			)
		);
		$imageLibraryFilter = new ImageLibraryFilter();
		$imageLibraryFilter->select(
			array(
				ImageLibraryFilter::CONDITION_FIELD_NAME_ID,
				ImageLibraryFilter::CONDITION_FIELD_NAME_FILENAME,
			)
		);
		$videoFilter->order(VideoFilter::CONDITION_FIELD_NAME_PUBLISHDATE,false);
		$videoFilter->limit(0,self::VIDEO_COUNT);
		$videoFilter->where(VideoFilter::CONDITION_FIELD_NAME_PUBLISHED,Condition::CONDITION_EQUAL,1);
		$videoFilter->leftJoin($imageLibraryFilter,null,VideoFilter::CONDITION_FIELD_NAME_PICID,ImageLibraryFilter::CONDITION_FIELD_NAME_ID,VideoFilter::CONDITION_FIELD_NAME_PIC_URL,false);
	
		$videoList = \haibao\cms\business\Video::getVideoListByFilter($videoFilter);
		$videoArr = array();
		foreach($videoList as $list){
			$publishTime = strtotime($list->PublishDate->format('Y-m-d H:i:s'));
			$videoArr[$publishTime] = $list->toArray();
			$videoArr[$publishTime]['PicUrl'] = $list->PicUrl->Filename;
			$videoArr[$publishTime]['Type'] = 3;
			$videoArr[$publishTime]['Url'] = $list->VideoUrl;
			$videoArr[$publishTime]['IsVideo'] = 1;
		}
		krsort($videoArr);
		return $videoArr;
	}
	
	/**
	 * 获取专题列表
	 */
	/* private function getFeatureList(){
		$featureArr = array();
		$featureFilter = new FeaturePublishFilter;
		$imageLibraryFilter = new ImageLibraryFilter();
		$featureFilter->select(array(
				FeaturePublishFilter::CONDITION_FIELD_NAME_ID,
				FeaturePublishFilter::CONDITION_FIELD_NAME_PICTURE_ID,
				FeaturePublishFilter::CONDITION_FIELD_NAME_URL,
				FeaturePublishFilter::CONDITION_FIELD_NAME_FEATURE_NAME,
				FeaturePublishFilter::CONDITION_FIELD_NAME_PUBLISH_TIME
		));
		 
		$imageLibraryFilter->select(array(
				ImageLibraryFilter::CONDITION_FIELD_NAME_FILENAME,
		));
		$featureFilter->order(FeaturePublishFilter::CONDITION_FIELD_NAME_PUBLISH_TIME,false);
		$featureFilter->where(FeaturePublishFilter::CONDITION_FIELD_NAME_PICTURE_ID, Condition::CONDITION_NOTEQUAL, '');
		$featureFilter->leftJoin($imageLibraryFilter,$featureFilter,FeaturePublishFilter::CONDITION_FIELD_NAME_PICTURE_ID,ImageLibraryFilter::CONDITION_FIELD_NAME_ID,FeaturePublishFilter::CONDITION_FIELD_NAME_IMAGE,false);
		$featureFilter->where(FeaturePublishFilter::CONDITION_FIELD_NAME_IS_H5, Condition::CONDITION_EQUAL, 0);
		$featureFilter->where(FeaturePublishFilter::CONDITION_FIELD_NAME_URL, Condition::CONDITION_NOT_LIKE, '%/brand/%');
		$featureFilter->where(FeaturePublishFilter::CONDITION_FIELD_NAME_STATUS, Condition::CONDITION_EQUAL, FeatureModel::STATUS_PUBLISH);
		$featureFilter->limit(self::FEATURE_COUNT);
		$data = new \haibao\cms\data\FeaturePublish();
		$featureList = $data->getAll($featureFilter);
		foreach($featureList as $list){
			$publishTime = strtotime($list->PublishTime->format('Y-m-d H:i:s'));
			$featureArr[$publishTime] = $list->toArray();
			$featureArr[$publishTime]['Title'] = $list->FeatureName;
			$featureArr[$publishTime]['PicUrl'] = $list->Image->Filename;
			$featureArr[$publishTime]['Type'] = 3;
			$featureArr[$publishTime]['IsVideo'] = 0;
		}
		krsort($featureArr);
		return $featureArr;
	} */
	
	private function getFeatureList(){
	    $ztSpecialFilter = new ZtSpecialFilter();
	    $ztSpecialFilter->select(array(
	        ZtSpecialFilter::CONDITION_FIELD_NAME_ID,
	        ZtSpecialFilter::CONDITION_FIELD_NAME_DESCRIPTION,
	        ZtSpecialFilter::CONDITION_FIELD_NAME_PIC,
	        ZtSpecialFilter::CONDITION_FIELD_NAME_TITLE,
	        ZtSpecialFilter::CONDITION_FIELD_NAME_URL,
	        ZtSpecialFilter::CONDITION_FIELD_NAME_LIST_DATE
	    ));
	    $ztSpecialFilter->order(ZtSpecialFilter::CONDITION_FIELD_NAME_LIST_DATE,false);
	    $ztSpecialFilter->where(ZtSpecialFilter::CONDITION_FIELD_NAME_LIST_SHOW, Condition::CONDITION_EQUAL, ZtSpecialData::LIST_SHOW_ENABLE);
	    //$ztSpecialFilter->where(ZtSpecialFilter::CONDITION_FIELD_NAME_TAG, Condition::CONDITION_EQUAL, $classId);
	    $ztSpecialFilter->where($ztSpecialFilter::CONDITION_FIELD_NAME_URL, Condition::CONDITION_NOT_LIKE, '%/brand/%');
	    $ztSpecialFilter->limit(self::FEATURE_COUNT);
	     
	    $featureList = \haibao\www\business\ZtSpecial::getFeatureList($ztSpecialFilter);
	    foreach($featureList as $list){
			$publishTime = strtotime($list->ListDate->format('Y-m-d H:i:s'));
			$featureArr[$publishTime] = $list->toArray();
			$featureArr[$publishTime]['Title'] = $list->Title;
			$featureArr[$publishTime]['PicUrl'] = $list->Pic;
			$featureArr[$publishTime]['Type'] = 3;
			$featureArr[$publishTime]['IsVideo'] = 0;
		}
		krsort($featureArr);
		return $featureArr;
	}
	
	private function paramPackageArr(){
		
		$paramArr = array();
		foreach($this->packageArr as $key=>$arr){
			$temp = array();
			$entity = $arr['Entity'];
			switch ($arr['Type']){
				case 1:
					$temp['Type'] = $arr['Type'];
					$temp['Id'] = $entity->Id;
					$temp['Title'] =  $entity->ShortTitle;
					$temp['PicUrl'] = $entity->ArticleProImage;
					$temp['IsVideo'] = $arr['IsVideo'];
					$temp['Url'] = self::getArticleUrl($entity->PublishId,$entity->ArticleTag->ParentId);
					
					break;
				case 2:
					$temp['Type'] = $arr['Type'];
					$temp['Id'] = $entity->Id;
					$temp['Title'] =  $entity->Title;
					$temp['PicUrl'] = $entity->ImageCollectivePro;
					$temp['IsVideo'] = $arr['IsVideo'];
					$temp['Url'] = self::getArticleUrl($entity->Id,$entity->ImageCollectiveTag->ParentId);				
					
					break;
				case 3:
					$temp['Type'] = $arr['Type'];
					$temp['Id'] = $entity->Id;
					$temp['Title'] =  $entity->FeatureDefine->FeatureName;
					$temp['PicUrl'] = $entity->Image->Filename;
					$temp['IsVideo'] = $arr['IsVideo'];
					$temp['Url'] = $entity->Url;
					break;
				case 4:
					$temp['Type'] = $arr['Type'];
					$temp['Id'] = $entity->Id;
					$temp['Title'] =  $entity->Title;
					$temp['PicUrl'] = $entity->PicUrl->Filename;
					$temp['IsVideo'] = $arr['IsVideo'];
					$temp['Url'] = $entity->VideoUrl;
					break;
				case 9:
					$temp['Type'] = $arr['Type'];
					$temp['Id'] = $entity->Id;
					$temp['Title'] =  $entity->ExclusiveName;
					$temp['PicUrl'] = $entity->PicUrl;
					$temp['IsVideo'] = $arr['IsVideo'];
					$temp['Url'] = $entity->ExclusiveUrl;
					break;
				default:
					break;
			}

			$paramArr[$key] = $temp;
		}
		return $paramArr;
	}
	
}